package com.hongxin.nw.netty.down;


import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.info.Mrena1_13;
import com.hongxin.nw.netty.model.packing.ASDUcsdna1;
import com.hongxin.nw.netty.model.packing.info.Csdna1_19;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;

/**
 * @remark 远程启动充电执行结果确认
 * @author: GuoXiaolin
 * @date 2019/6/21 15:45
 * @Version 1.0
 * @Descibe
 */
public class ChargeConfirmReq implements IChannelRequest {

    @Override
    public String doPacking(Transfer transfer) throws Exception {

        String code = transfer.getCode();
        Mrena1_13 dataItem = (Mrena1_13)transfer.getObj();
        Csdna1_19 csdna1_19 = new Csdna1_19();
        csdna1_19 = csdna1_19.setCo(code)
                .setChargeIdentify(dataItem.getChargeIdentify())
                .setSuccessIdentify(0);

        ASDUcsdna1 mm = new ASDUcsdna1();
        mm.setLogType(String.valueOf(TypeIdentif.RECORD_CHARGE_EXECUTE_CONFIRM.getInt()));
        mm.setSendData(csdna1_19.packing());

//        as.setType(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt());
//        as.setVsq(0, StaticValue.SQ0);
//        as.setCot(TypeIdentif.COT_ACTIVATION_CONFIRM.getInt());
//        as.setInfoadr(Integer.valueOf(dataItem.getChargeIdentify()));
//        as.setStaadr(AsduStartPools.getAsduStart(code).getStatAddress());

        String as = FieldPack.packASDU(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt(),
                TypeIdentif.COT_ACTIVATION_CONFIRM.getInt(),
                AsduStartPools.getAsduStart(code).getStatAddress(),Integer.valueOf(dataItem.getChargeIdentify()),
                0, StaticValue.SQ0);

        String data = as + mm.getHexData();

//        APCI ac = new APCI();
//        ac.setIdentifyStart(TypeIdentif.IDENTIFY_START.getInt())
//                .setApduLength(as.getHexData().length()/2)
//                .setContarea(TypeIdentif.FORMAT_I.getStr(),StaticValue.U_START_V);
//
//        return ac.getHexData() + as.getHexData();

        String ac = FieldPack.packAPCI(data.length(), TypeIdentif.FORMAT_I.getStr(), StaticValue.U_START_V);
        return ac + data;

    }

}
